package com.example.bgImageSear.user.controller;

import com.example.bgImageSear.commons.Result;
import com.example.bgImageSear.user.entity.BgDataEntity;
import com.example.bgImageSear.user.service.IBgDataService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController

//图片操作类
@RequestMapping("/bgData")
public class
BgDataController {
    @Resource
    private IBgDataService bgDataService;

    //获取所有图片
    @RequestMapping("/list")
    public Result list() {
        List<BgDataEntity> bgDataList = bgDataService.getBgDataList();
        if (null == bgDataList) {
            return new Result(0, "获取所有图片失败", null);
        }
        return new Result(1, "获取所有图片成功", bgDataList);
    }

    @RequestMapping("/imageId")
    public Result getImageById(@RequestParam Integer id) {
        List<BgDataEntity> imageById = bgDataService.getImageById(id);
        if (imageById == null) {
            return new Result(0, "查询失败", null);
        }
        return new Result(1, "查询成功!", imageById);
    }

    //根据模糊查询筛选需要的图片
    @RequestMapping(value = "/search", method = RequestMethod.POST)
    public Result tagList(@RequestParam String tag) {
        List<BgDataEntity> bgDataEntities = bgDataService.getBgDataByTag(tag);
        if (null == bgDataEntities) {
            return new Result(0, "获取筛选后的图片失败", null);
        }
        return new Result(1, "获取筛选后的图片成功", bgDataEntities);
    }
}
